<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reset Password</title>
    <script type="text/javascript">
        function getQueryParam(param) {
            const urlParams = new URLSearchParams(window.location.search);
            return urlParams.get(param);
        }

        window.onload = function() {
            const token = getQueryParam('token');
            if (token) {
                document.getElementById('token').value = token;
            } else {
                alert("Token parameter is missing.");
            }
        };

        async function submitForm(event) {
            event.preventDefault(); // 防止默认的表单提交行为

            const token = document.getElementById('token').value;
            const newPassword = document.getElementById('new_password').value;

            const response = await fetch('/reset_password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({token: token, new_password: newPassword})
            });

            const data = await response.json();
            if (response.ok) {
                alert(data.message || "Password reset successful");
            } else {
                alert(data.message || "Failed to reset password");
            }
        }
    </script>
</head>
<body>
    <h1>Password Reset</h1>
    <form onsubmit="submitForm(event)">
        <!-- 隐藏的token字段 -->
        <input type="hidden" id="token" name="token">
        <label for="new_password">New Password:</label><br>
        <input type="password" id="new_password" name="new_password" required><br><br>
        <input type="submit" value="Reset Password">
    </form>
</body>
</html>